<HTML><HEAD>
<!--
    ----------------
    Color Match Game
    ----------------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

//------------------RANDOM NUMBERS----------------------------
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0


// Return a Random Integer between 0 and N-1
function random(n)
{
    if (js_iseed == 0)
    {
        now = new Date()
        js_iseed = now.getHours() + now.getMinutes() * 60 
                    + now.getSeconds() * 3600
    }
    js_iseed1 = js_iseed / js_m2
    js_iseed2 = js_iseed % js_m2
    var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) * 
                js_m2 + (js_iseed2 * js_mult2)) % js_m1
    js_iseed = (tmp + 1) % js_m1
    return (Math.floor((js_iseed/js_m1) * n))
}


// --------------------Hexadecimal Conversion---------------------

// convert a single digit (0 - 16) into hex
function enHex(aDigit)
{
    return("0123456789ABCDEF".substring(aDigit, aDigit+1))
}

// convert a hex digit into decimal
function deHex(aDigit)
{
    return("0123456789ABCDEF".indexOf(aDigit))
}

// Convert a 24bit number to hex
function toHex(n)
{
    return (enHex((0xf00000 & n) >> 20) +
            enHex((0x0f0000 & n) >> 16) +
            enHex((0x00f000 & n) >> 12) +
            enHex((0x000f00 & n) >>  8) +
            enHex((0x0000f0 & n) >>  4) +
            enHex((0x00000f & n) >>  0))
}

// Convert a six character hex to decimal
function toDecimal(hexNum)
{
    var tmp = ""+hexNum.toUpperCase()
    while (tmp.length < 6) tmp = "0"+tmp
    
    return ((deHex(tmp.substring(0,1)) << 20) +
            (deHex(tmp.substring(1,2)) << 16) + 
            (deHex(tmp.substring(2,3)) << 12) +
            (deHex(tmp.substring(3,4)) << 8) +
            (deHex(tmp.substring(4,5)) << 4) +
            (deHex(tmp.substring(5,6))))
}

// --------------------JSCcolor Object---------------------

// Returns a JavaScript integer representing a JSCcolor object's color
function rawColor()
{
    return (this.red << 16) + (this.green << 8) + this.blue
}

// Returns a hex string representing a JSCcolor object's color
function hexColor()
{
    return toHex(this.rawColor())
}

// Set the JSCcolor object's color to that in a hex string.
// This routine does not add the Ox prefix.
function setColor(aString)
{
    var tmp = toDecimal(aString)

    this.red = (0xff0000 & tmp) >> 16
    this.green = (0xff00 & tmp) >> 8
    this.blue = (0xff & tmp)
    
    return this
}

// Set the JSCcolor object's color to an integer
function setDecimalColor(aNumber)
{
    var tmp = toHex(aNumber)

    this.red = eval('0x'+tmp.substring(0,2))
    this.green = eval('0x'+tmp.substring(2,4))
    this.blue = eval('0x'+tmp.substring(4,8))
    
    return this
}

// Adjust a JSCcolor object's color (red, green or blue) by an offset
function adjustColor(aColor, anOffset)
{
    this[aColor] += anOffset
    if (this[aColor] > 0xff) this[aColor] = 0xff
    if (this[aColor] < 0x0) this[aColor] = 0x0
}

// Adjust one or more of a JSCcolor object's colors by an offset
function adjustColors(colString, anOffset)
{
    var cs = colString.toUpperCase()
    
    if (cs.indexOf('R') != -1) this.adjustColor('red', anOffset)
    if (cs.indexOf('G') != -1) this.adjustColor('green', anOffset)
    if (cs.indexOf('B') != -1) this.adjustColor('blue', anOffset)
}

// JSCcolor object constructor
function JSCcolor(r, g, b)
{
    // set properties
    this.red = r
    this.green = g
    this.blue = b
    
    // set methods
    this.rawColor = rawColor
    this.hexColor = hexColor
    this.adjustColor = adjustColor
    this.adjustColors = adjustColors
    this.setColor = setColor
    this.setDecimalColor = setDecimalColor

    return this
}

// --------------------GAME ROUTINES---------------------

// These color objects will store our current color
var myColor = new JSCcolor(0x80, 0x80, 0x80)
var gColor = new JSCcolor(0x80, 0x80, 0x80)
var game = false

// Set the left hand window to start a game.
function setGame()
{
    gColor.red = random(256)
    gColor.green = random(256)
    gColor.blue = random(256)
    parent.JCgame2.document.bgColor = gColor.hexColor()
    if (!game) game = true
}

// Give a clue
function hint()
{
    var dr = myColor.red - gColor.red
    var dg = myColor.green - gColor.green
    var db = myColor.blue - gColor.blue
    if (!game)    return alert("Start the game first!")

    var which = random(3) + 1
    if (which == 1)
    {
        if (dr != 0) return alert((dr > 0) ? 'Less Red' : 'More Red')
    }
    if (which == 2)
    {
        if (dg != 0) return alert((dg > 0) ? 'Less Green' : 'More Green')
    }
    if (which == 3)
    {
        if (db != 0) return alert((db > 0) ? 'Less Blue' : 'More Blue')
    }
    return alert('One of your colors is correct')
}

// Do the actual adjustment
function doAdjust(aColor, anOffset)
{
    myColor.adjustColor(aColor, anOffset)
    parent.JCgame1.document.bgColor = myColor.hexColor()

    if (game)
    {    
        if ((gColor.red == myColor.red) &&
            (gColor.green == myColor.green) &&
            (gColor.blue == myColor.blue))
        {
            alert("You matched the color!!! (Now try a new one...)")
            setGame()
        }
    }
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077">
    
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/SPICE.JPG" WIDTH=37 HEIGHT=72
ALIGN = LEFT>Color Matching Game</H1></FONT>

<BLOCKQUOTE><FONT SIZE=4>
    Use the control arrows to adjust the color of the left window
    to match the right window.<p>
</FONT></BLOCKQUOTE>

<CENTER>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Play Game" onClick="setGame()">
<INPUT TYPE="BUTTON" VALUE="Hint" onClick="hint()">
</FORM>

<IMG SRC="../GRAFX/ARROWS.GIF" width=164 height=101 usemap="#map" border=0>
<MAP name="map">

<AREA SHAPE=rect HREF="javascript:doAdjust('blue', 0x10)"  COORDS="116, 3, 153,27">
<AREA SHAPE=rect HREF="javascript:doAdjust('blue', 0x01)"   COORDS="116,28, 153,47">
<AREA SHAPE=rect HREF="javascript:doAdjust('blue', -0x01)"  COORDS="116,48, 153,69">
<AREA SHAPE=rect HREF="javascript:doAdjust('blue', -0x10)" COORDS="116,70, 153,97">

<AREA SHAPE=rect HREF="javascript:doAdjust('green', 0x10)"  COORDS="62, 3, 97,27">
<AREA SHAPE=rect HREF="javascript:doAdjust('green', 0x01)"   COORDS="62,28, 97,47">
<AREA SHAPE=rect HREF="javascript:doAdjust('green', -0x01)"  COORDS="62,48, 97,69">
<AREA SHAPE=rect HREF="javascript:doAdjust('green', -0x10)" COORDS="62,70, 97,97">

<AREA SHAPE=rect HREF="javascript:doAdjust('red', 0x10)"   COORDS="10, 3, 44,27">
<AREA SHAPE=rect HREF="javascript:doAdjust('red', 0x01)"    COORDS="10,28, 44,47">
<AREA SHAPE=rect HREF="javascript:doAdjust('red', -0x01)"   COORDS="10,48, 44,69">
<AREA SHAPE=rect HREF="javascript:doAdjust('red', -0x10)"  COORDS="10,70, 44,97">

</MAP>
</CENTER>

<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>